package com.jxwifi.cloud.quickclean.welcome;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.os.Build;
import android.os.CountDownTimer;
import android.os.Handler;
import android.os.Message;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

import com.alibaba.fastjson.JSON;
import com.bumptech.glide.Glide;
import com.jxwifi.cloud.quickclean.R;
import com.jxwifi.cloud.quickclean.app.LocationGaoDe;
import com.jxwifi.cloud.quickclean.app.UrlPool;
import com.jxwifi.cloud.quickclean.bean.AdvertBean;
import com.jxwifi.cloud.quickclean.model.AuzImpl;
import com.jxwifi.cloud.quickclean.okhttp.Params;
import com.jxwifi.cloud.quickclean.popwindow.PopProtocol;
import com.jxwifi.cloud.quickclean.utils.NetUtils;
import com.popwindow.PopWindowUtil;
import com.popwindow.Popwindows;

import java.util.ArrayList;
import java.util.List;

public class WCPresenter {

    private String TAG = this.getClass().getSimpleName();
    private static final int SPLASH_DIPLAY_LENGHT = 1500;
    private final int SDK_PERMISSION_REQUEST = 127;
    public Context mContext;
    public Popwindows popwindow;
    private SharedPreferences sharedPreferences;
    private AuzImpl mImpl;
    private List<AdvertBean> mListAdvert;//广告集合
    public String imageUrl;
    public String positionUrl;
    public String title;


    protected static final String EXITAPP = "exit_app";

    private WCView mView;

    public WCPresenter(Context context,WCView mView){
        this.mContext=context;
        this.mView=mView;
        mImpl = new AuzImpl();

    }

    public void initHeadLayout(){
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = mView.getVindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);
        }
    }

    public void getPersimmions() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            ArrayList<String> permissions = new ArrayList<String>();
            /***
             * 定位权限为必须权限，用户如果禁止，则每次进入都会申请
             * 其他需要到很重要的限制在此处都要申请
             */
            // 定位精确位置*
            if (mContext.checkSelfPermission(android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                permissions.add(android.Manifest.permission.ACCESS_FINE_LOCATION);
            }
            if (mContext.checkSelfPermission(android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                permissions.add(android.Manifest.permission.ACCESS_COARSE_LOCATION);
            }
            if (mContext.checkSelfPermission(android.Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
                permissions.add(android.Manifest.permission.CAMERA);
            }
            // 读写权限
            if (mContext.checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
                permissions.add(android.Manifest.permission.WRITE_EXTERNAL_STORAGE);
            }
            if (permissions.size() > 0) {
                //mContext.requestPermissions(permissions.toArray(new String[permissions.size()]), SDK_PERMISSION_REQUEST);
                mView.getrequestPermissions(permissions.toArray(new String[permissions.size()]), SDK_PERMISSION_REQUEST);
            } else {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        firstRun();
                    }
                }, SPLASH_DIPLAY_LENGHT);
            }
        } else {
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    firstRun();
                }
            }, SPLASH_DIPLAY_LENGHT);
        }
    }

    public void onRequestPermissionsResult() {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                firstRun();
            }
        }, SPLASH_DIPLAY_LENGHT);
    }

    private void firstRun() {
        sharedPreferences = mContext.getSharedPreferences("FirstRun",0);
        Boolean first_run = sharedPreferences.getBoolean("First",true);
        if (first_run){
            //Toast.makeText(this,"第一次",Toast.LENGTH_LONG).show();
            protocol();
        }else {
            //Toast.makeText(this,"不是第一次",Toast.LENGTH_LONG).show();
            //getjump();
            Log.i(TAG,"firstRun()false ==> imageUrl = "+imageUrl+" positionUrl = "+positionUrl+" title = "+title);
            mView.getjump();

        }
    }


    public void protocol(){
        PopProtocol mDodule=new PopProtocol(mContext, new PopProtocol.PopProtocolListener() {
            @Override
            public void PopDisagree() {
                Glide.get(mContext).clearMemory();
                LocalBroadcastManager.getInstance(mContext.getApplicationContext()).sendBroadcast(new Intent(EXITAPP));
                System.gc();
            }

            @Override
            public void PopAgree() {
                sharedPreferences.edit().putBoolean("First",false).commit();
                mView.getjump();
            }
        });
        if (popwindow == null){
            popwindow = PopWindowUtil.getPopupWindow(mDodule.getView(), ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup
                    .LayoutParams.MATCH_PARENT);
            mDodule.setPopwindow(popwindow);
            popwindow.show(mView.getWelcomeImages(), Popwindows.Location.BOTTOM);
        }

    }

    //获取启动广告数据请求
    public void initAdvert() {
        Params params = new Params();
        Log.i(TAG, "province" + LocationGaoDe.getProvince() + " city" + LocationGaoDe.getCity() + " area" + LocationGaoDe.getArea());
        params.add("area", LocationGaoDe.getArea());//area
        params.add("city", LocationGaoDe.getCity());//city
        params.add("province", LocationGaoDe.getProvince());//province
        params.add("purposeTypeEnum","LAUNCH_AD");
        mImpl.auzpost(UrlPool.POST_INDEX_ADVERT, params, new AuzImpl.AuzListener() {
            @Override
            public void AuzSuccess(int code, String data) {
                //AdvertBeani
                if (mListAdvert!=null){
                    mListAdvert.clear();
                }
                mListAdvert = JSON.parseArray(data, AdvertBean.class);
                Log.i(TAG,"initAdvert() ==> mListAdvert = "+JSON.toJSONString(mListAdvert)+" mListAdvert.size() = "+mListAdvert.size());
                imageUrl = mListAdvert.get(0).getImageUrl();
                positionUrl = mListAdvert.get(0).getPositionUrl();
                title = mListAdvert.get(0).getTitle();
                Log.i(TAG,"initAdvert() ==> imageUrl = "+imageUrl+" positionUrl = "+positionUrl+" title = "+title);

            }

            @Override
            public void AuzFailure(int errorCode, String msg) {
                mView.onFailure(errorCode, msg);
            }
        });
    }

}
